<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RedisToMysql extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'redisToMysql';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'redis 同步到mysql';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $redis = app("redis");

        $key = "msg:account:*";
//        $key = "msg:device:*";
        $accounts = $redis -> keys($key);
        $data = [];
        $now = Carbon::now();
        foreach ($accounts as $account){
            $account = $redis -> hgetall($account);
            $account['deleted_at'] =$now;
            $account['failed_at'] = $now;
//            $account['Record_at'] = ;
            $account['created_at'] = $now;
            $account['updated_at'] = $now;
            $data[] = $account;
        }
//        $data = array_chunk($data,1000);
        foreach ($data as $item){
            DB::table("account_copy") -> insert($item);
//            DB::table("device_copy") -> insert($item);
        }
    }
}
